package com.adams.action.view;

import java.io.InputStream;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.adams.access.db.bean.DicColumn;
import com.adams.common.BaseAction;
import com.adams.core.exception.BusinessException;
import com.adams.custom.page.core.dto.DicColumnDto;
import com.adams.custom.page.core.service.DicTableService;
import com.adams.custom.page.mgt.service.ViewParseService;
import com.adams.custom.page.util.ExcelUtil;
import com.adams.custom.page.util.ViewConstant;
import com.adams.custom.page.util.ViewUtil;

/**
 * 
 * File Name : ViewAction.java
 * 
 * @Description : 界面组件action类
 * @author 904370
 */
public class ViewAction extends BaseAction
{

	private static final long serialVersionUID = 7652146782094447813L;

	Logger logger = Logger.getLogger(ViewAction.class.getName());

	protected ViewParseService viewParseService;

	protected DicTableService dicTableService;

	protected long menuId;

	protected String gridPanelId;

	protected String viewEventId;

	protected String formPanelId;

	protected String viewItemId;

	protected String searchPanelId;

	public void setViewParseService(ViewParseService viewParseService)
	{
		this.viewParseService = viewParseService;
	}

	public void setMenuId(long menuId)
	{
		this.menuId = menuId;
	}

	public void setGridPanelId(String gridPanelId)
	{
		this.gridPanelId = gridPanelId;
	}

	public void setViewEventId(String viewEventId)
	{
		this.viewEventId = viewEventId;
	}

	public void setFormPanelId(String formPanelId)
	{
		this.formPanelId = formPanelId;
	}

	public void setViewItemId(String viewItemId)
	{
		this.viewItemId = viewItemId;
	}

	public void setSearchPanelId(String searchPanelId)
	{
		this.searchPanelId = searchPanelId;
	}

	public ViewParseService getViewParseService()
	{
		return viewParseService;
	}

	public long getMenuId()
	{
		return menuId;
	}

	public String getGridPanelId()
	{
		return gridPanelId;
	}

	public String getViewEventId()
	{
		return viewEventId;
	}

	public String getFormPanelId()
	{
		return formPanelId;
	}

	public String getViewItemId()
	{
		return viewItemId;
	}

	public String getSearchPanelId()
	{
		return searchPanelId;
	}

	public DicTableService getDicTableService()
	{
		return dicTableService;
	}

	public void setDicTableService(DicTableService dicTableService)
	{
		this.dicTableService = dicTableService;
	}

	/**
	 * 显示布局
	 * 
	 * @return json格式字符串
	 * @throws Exception
	 */
	public String getView() throws Exception
	{
		logger.info("getView method run begin.");
		// Map map = viewParseService.getParseViewByMenuId(getUserId(), menuId);
		// write(JSONObject.fromObject(map).toString());
		write(viewParseService.getParseViewJSONByMenuId(getUserId(), menuId));
		logger.info("getView method run end.");
		return null;
	}

	/**
	 * Description : 显示查询条字段信息
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	public String listSearchItem() throws Exception
	{
		logger.info("listSearchItem method run begin.");
		// Map map = viewParseService.getParseSearchItems(getUserId(), menuId,
		// searchPanelId);
		// write(JSONObject.fromObject(map).toString());
		write(viewParseService.getParseSearchItemsJSON(getUserId(), menuId,
				searchPanelId));
		logger.info("listSearchItem method run end.");
		return null;
	}

	/**
	 * 显示元素
	 * 
	 * @return json格式字符串
	 * @throws Exception
	 */
	public String getGridStructure() throws Exception
	{
		logger.info("getGridStructure method run begin.");
		if (ViewUtil.isNuLLStr(gridPanelId))
		{
			gridPanelId = this.request.getParameter("gridPanelId");
		}
		// Map map = viewParseService.getParseGrids(getUserId(), gridPanelId);
		// write(JSONObject.fromObject(map).toString());
		write(viewParseService.getParseGridsJSON(getUserId(), gridPanelId));
		logger.info("getGridStructure method run end.");
		return null;
	}

	/**
	 * 显示数据
	 * 
	 * @return json格式字符串
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public String getGridData() throws Exception
	{
		logger.info("getGridData method run begin.");
		if (ViewUtil.isNuLLStr(gridPanelId))
		{
			gridPanelId = request.getParameter("gridPanelId");
		}
		if (ViewUtil.isNuLLStr(searchPanelId))
		{
			searchPanelId = request.getParameter("searchPanelId");
		}
		Map inputMap = ViewAction.requestParameterMap2Map(request);
		Map map = viewParseService.getParseGridDatas(getUserId(), gridPanelId,
				searchPanelId, inputMap);
		write(JSONObject.fromObject(map).toString());
		logger.info("getGridData method run end.");
		return null;
	}
	
	/**
	 * 
	 * Description 	: 根据 menuId 打开页面表单窗口，用来获取要审核的数据的面板
	 *
	 * @return
	 * @throws Exception
	 *
	 */
	@SuppressWarnings("unchecked")
	public String getFormByMenuId()  throws Exception{
		logger.info("getFormByMenuId method run begin.");
		Map	inputMap = ViewAction.requestParameterMap2Map(request);
		String str = viewParseService.getParseFormByMenuId(getUserId(), menuId,inputMap);
		JSONObject jsonResp = JSONObject.fromObject(str);
		jsonResp.put("postData", JSONObject.fromObject(inputMap).toString());
		request.setAttribute("jsonResp", jsonResp);
		logger.info("getFormByMenuId method run end.");
		return SUCCESS;
	}

	/**
	 * 打开面板窗口
	 * 
	 * @return json格式字符串
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public String getForm() throws Exception
	{
		logger.info("getForm method run begin.");
		if (ViewUtil.isNuLLStr(viewEventId))
		{
			viewEventId = request.getParameter("viewEventId");
		}
		List<Map> mapList = ViewAction.getDataMap(request);
		Map inputMap = null;
		if (mapList.size() > 0)
		{
			inputMap = ViewAction.getDataMap(request).get(0);
		}
		Map map = viewParseService.getParseViewByEventId(getUserId(), menuId,
				viewEventId, inputMap);
		write(JSONObject.fromObject(map).toString());
		logger.info("getForm method run end.");
		return null;
	}

	/**
	 * 根据页面按钮打开面板窗口
	 * 
	 * @return json格式字符串
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public String getViewByEvent() throws Exception
	{
		logger.info("getCommQueryGridWin method run begin.");
		List<Map> mapList = ViewAction.getDataMap(request);
		Map inputMap = new HashMap();
		if (mapList.size() > 0)
		{
			inputMap = ViewAction.getDataMap(request).get(0);
		}
		Map relationParam = getGridrelationparam(request);
		if (null != relationParam)
		{
			inputMap.put(ViewConstant.JSON_GRID_RELATION_PARAM, relationParam);
		}
		Map map = viewParseService.getParseViewByEventId(getUserId(), menuId,
				viewEventId, inputMap);
		write(JSONObject.fromObject(map).toString());
		logger.info("getCommQueryGridWin method run end.");
		return null;

	}

	/**
	 * 打开内容项选择面板窗口
	 * 
	 * @return json格式字符串
	 * @throws Exception
	 */
	public String getCompositeSelectWin() throws Exception
	{
		logger.info("getCompositeSelectWin method run begin.");
		String json = viewParseService.getParseViewJSONByItemId(getUserId(),
				viewItemId);
		write(json);
		logger.info("getCompositeSelectWin method run end.");
		return null;

	}

	/**
	 * 获得操作面板详细信息
	 * 
	 * @return json格式字符串
	 * @throws Exception
	 */
	public String getOperatePanelDetail() throws Exception
	{
		logger.info("getOperatePanelDetail method run begin.");
		String json = viewParseService.getParseOperatePanelDetailJSONByPanelid(
				getUserId(), formPanelId);
		write(json);
		logger.info("getOperatePanelDetail method run end.");
		return null;
	}

	/**
	 * 动态获得下拉查询列表
	 * 
	 * @return json格式字符串
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public String getDynamicCombo() throws Exception
	{
		logger.info("getDynamicCombo method run begin.");
		if (ViewUtil.isNuLLStr(viewItemId))
		{
			viewItemId = request.getParameter("viewItemId");
		}
		Map map = viewParseService.getParseDynamicCombo(getUserId(), menuId,
				viewItemId);
		write(JSONObject.fromObject(map).toString());
		logger.info("getDynamicCombo method run end.");
		return null;
	}

	/**
	 * Description : 增加数据
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String addDataByViewForm() throws Exception
	{
		logger.info("addDataByViewForm method run begin.");
		if (ViewUtil.isNuLLStr(formPanelId))
		{
			formPanelId = request.getParameter("formPanelId");
		}
		Map inputMap = ViewAction.requestParameterMap2Map(request);
		Map map = viewParseService.saveOrUpdateDataInViewForm(getUserId(),
				menuId, formPanelId, inputMap);
		if (map.get("success") != null
				&& ((Boolean) map.get("success")) == true)
		{
			map.put("msg", "保存成功");
		}
		write(JSONObject.fromObject(map).toString());
		logger.info("addDataByViewForm method run end.");
		return null;
	}

	/**
	 * Description : 更新数据
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String updateDataByViewForm() throws Exception
	{
		logger.info("updateDataByViewForm method run begin.");
		if (ViewUtil.isNuLLStr(formPanelId))
		{
			formPanelId = request.getParameter("formPanelId");
		}
		Map inputMap = ViewAction.requestParameterMap2Map(request);
		Map map = viewParseService.saveOrUpdateDataInViewForm(getUserId(),
				menuId, formPanelId, inputMap);
		if (map.get("success") != null
				&& ((Boolean) map.get("success")) == true)
		{
			map.put("msg", "保存成功");
		}
		write(JSONObject.fromObject(map).toString());
		logger.info("updateDataByViewForm method run end.");
		return null;
	}

	/**
	 * Description : 更新数据
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String updateDataByGridPanel() throws Exception
	{
		logger.info("updateDataByGridPanel method run begin.");
		if (ViewUtil.isNuLLStr(viewEventId))
		{
			viewEventId = request.getParameter("viewEventId");
		}
		Map inputMap = ViewAction.requestParameterMap2Map(request);
		List<Map> mapList = new ArrayList<Map>();
		mapList.add(inputMap);
		Map map = viewParseService.saveOrUpdateDataInGridPanel(getUserId(),
				menuId, viewEventId, mapList);
		write(JSONObject.fromObject(map).toString());
		logger.info("updateDataByGridPanel method run end.");
		return null;
	}

	/**
	 * Description : 删除数据
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String deleteDataByViewForm() throws Exception
	{
		logger.info("deleteDataByViewForm method run begin.");
		if (ViewUtil.isNuLLStr(viewEventId))
		{
			viewEventId = request.getParameter("viewEventId");
		}
		List<Map> mapList = ViewAction.getDataMap(request);
		Map map = viewParseService.deleteDataByViewForm(getUserId(),
				viewEventId, mapList);
		write(JSONObject.fromObject(map).toString());
		logger.info("deleteDataByViewForm method run end.");
		return null;
	}

	/**
	 * Description : 得到详细数据
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String getDataByViewForm() throws Exception
	{
		logger.info("getDataByViewForm method run begin.");
		if (ViewUtil.isNuLLStr(formPanelId))
		{
			formPanelId = request.getParameter("formPanelId");
		}
		List<Map> mapList = ViewAction.getDataMap(request);
		Map inputMap = new HashMap();
		if (mapList.size() > 0)
		{
			inputMap = ViewAction.getDataMap(request).get(0);
		}

		Map map = viewParseService.getDataByViewForm(getUserId(), menuId,
				formPanelId, inputMap);
		write(JSONObject.fromObject(map).toString());
		logger.info("getDataByViewForm method run end.");
		return null;
	}

	/**
	 * Description : 把所有请求参数放入一个Map中
	 * 
	 * @param request
	 * @return
	 * 
	 */
	@SuppressWarnings("unchecked")
	public static Map requestParameterMap2Map(HttpServletRequest request)
	{
		Enumeration enu = request.getParameterNames();
		Map inputMap = new HashMap();
		while (enu.hasMoreElements())
		{
			String name = (String) enu.nextElement();
			inputMap.put(name, request.getParameter(name));
		}
		Map relationParam = getGridrelationparam(request);
		if (null != relationParam)
		{
			inputMap.put(ViewConstant.JSON_GRID_RELATION_PARAM, relationParam);
		}
		return inputMap;
	}

	/**
	 * Description : 把所有请求参数解码后放入一个Map中
	 * 
	 * @param request
	 * @return
	 * 
	 */
	@SuppressWarnings("unchecked")
	public static Map decodedRequestParameterMap2Map(HttpServletRequest request) throws Exception
	{
		Enumeration enu = request.getParameterNames();
		Map inputMap = new HashMap();
		while (enu.hasMoreElements())
		{
			String name = (String) enu.nextElement();
			String value = URLDecoder.decode(request.getParameter(name), "UTF-8");
			inputMap.put(name, value);
		}
		Map relationParam = getGridrelationparam(request);
		if (null != relationParam)
		{
			inputMap.put(ViewConstant.JSON_GRID_RELATION_PARAM, relationParam);
		}
		return inputMap;
	}
	
	@SuppressWarnings("unchecked")
	private static Map getGridrelationparam(HttpServletRequest request)
	{
		Map result = null;
		String paramStr = request
				.getParameter(ViewConstant.JSON_GRID_RELATION_PARAM);
		if (ViewUtil.isNuLLStr(paramStr))
		{
			return result;
		}
		JSONObject data = JSONObject.fromObject(paramStr);
		result = (Map) JSONObject.toBean(data, HashMap.class);
		return result;
	}

	/**
	 * Description : 得到界面组件回传的数据
	 * 
	 * @param request
	 * @return
	 * 
	 */
	@SuppressWarnings("unchecked")
	public static List<Map> getDataMap(HttpServletRequest request)
	{
		Map inputMap = requestParameterMap2Map(request);
		List<Map> mapList = ViewUtil.getMultiChoiceData(inputMap);
		return mapList;

	}

	/**
	 * Excel数据
	 * 
	 * @return null
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public String getExcelData() throws Exception
	{
		logger.info("getExcelData method run begin.");
		if (ViewUtil.isNuLLStr(viewEventId))
		{
			viewEventId = request.getParameter("viewEventId");
		}
		if (ViewUtil.isNuLLStr(searchPanelId))
		{
			searchPanelId = request.getParameter("searchPanelId");
		}
		Map inputMap = ViewAction.decodedRequestParameterMap2Map(request);
		this.getExcelDatas(null, inputMap);
		logger.info("getExcelData method run end.");
		return null;
	}

	protected String getExcelData(List dataList) throws BusinessException
	{
		logger.info("getExcelData method run begin.");
		if (ViewUtil.isNuLLStr(viewEventId))
		{
			viewEventId = request.getParameter("viewEventId");
		}
		if (ViewUtil.isNuLLStr(searchPanelId))
		{
			searchPanelId = request.getParameter("searchPanelId");
		}
		Map inputMap = ViewAction.requestParameterMap2Map(request);
		this.getExcelDatas(dataList, inputMap);
		logger.info("getExcelData method run end.");
		return null;
	}

	private void getExcelDatas(List dataList, Map inputMap)
			throws BusinessException
	{
		List recordList = null;
		logger.info("getExcelDatas method run begin.");
		if (dataList == null)
		{
			recordList = viewParseService.getParseExcelDatas(getUserId(),
					viewEventId, searchPanelId, inputMap);
		} else
		{
			recordList = viewParseService.getParseExcelDatasFromList(super
					.getUserId(), viewEventId, dataList);
		}
		wirteExcel(recordList);
		logger.info("getExcelDatas method run end.");
	}

	private void wirteExcel(List recordList) throws BusinessException
	{
		InputStream is = ExcelUtil.createExcelStream(recordList);
		writeExcelFile(is);
	}
}
